import router from '@/router'
import store from '@/store'
// 导入进度条Nprogress
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
const whiteList = ['/login', '/404']
// 前置导航守卫
router.beforeEach(async(to, from, next) => {
  NProgress.start()
  const token = store.getters.token
  if (token) {
    if (to.path === '/login') {
      next('/')
      NProgress.done()
    } else {
      if (!store.state.user.userInfo.userId) {
        // store.dispatch('user/reqLogin').then(res => {
        //   console.log('ok')
        // }).catch(err => {
        //   console.log(err)
        // })
        await store.dispatch('user/reqLogin')
        // console.log('ok', res)
      }
      next()
    }
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
      NProgress.done()
    }
  }
})
// 后置导航守卫
router.afterEach((to, from) => {
  NProgress.done()
})
